$(function() {

    const container = document.querySelector('#container');
    const signInButton = document.querySelector('#signIn');
    const signUpButton = document.querySelector('#signUp');
    // console.log(signUpButton, signInButton, container);
    signUpButton.addEventListener('click', () => {
        container.classList.add('right-panel-active')
            // $(".beforeLogin").remove()
    });
    signInButton.addEventListener('click', () => {
        container.classList.remove('right-panel-active')
            // $(".beforeLogin").remove()

    })

    const studentlink = document.querySelector('.student')
    const teacherlink = document.querySelector('.teacher')
    const instructorlink = document.querySelector('.instructor')
    var loginPath = 'http://127.0.0.1:3007/api/studentLogin'
    var signPath = 'http://127.0.0.1:3007/api/reguser'


    const title = document.querySelector('.usertitle')
    const title2 = document.querySelector('.usertitle2')


      //删除节点
    function deleteNode() {        
        $('input[class="sex"]').hide();    
        $('input[class="major"]').hide();    
        $('input[class="userclass"]').hide();    
        $('input[class="age"]').hide();    
        $('input[class="email"]').hide();    
        $('input[class="student_id"]').hide();    
    }  
    //添加节点
    function addNode() {     // window.location.replace("http://127.0.0.1:3007")
            
        $('input[class="sex"]').show();    
        $('input[class="major"]').show();    
        $('input[class="userclass"]').show();    
        $('input[class="age"]').show();    
        $('input[class="email"]').show();    
        $('input[class="student_id"]').show();  
    }





    // 点击端口事件
    studentlink.addEventListener('click', () => {
        title.innerHTML = "学生" + title.innerHTML.slice(title.innerHTML.length - 2, title.innerHTML.length)
        title2.innerHTML = "学生" + title2.innerHTML.slice(title.innerHTML.length - 2, title.innerHTML.length)
        localStorage.setItem('user', '学生')
        loginPath = 'http://127.0.0.1:3007/api/studentLogin'
            // console.log(path);
        addNode()
    })

    teacherlink.addEventListener('click', () => {
        title.innerHTML = "教师" + title.innerHTML.slice(title.innerHTML.length - 2, title.innerHTML.length)
        title2.innerHTML = "教师" + title2.innerHTML.slice(title.innerHTML.length - 2, title.innerHTML.length)
        localStorage.setItem('user', '教师')

        loginPath = 'http://127.0.0.1:3007/api/teacherLogin'
        deleteNode()

    })



    instructorlink.addEventListener('click', () => {

            title.innerHTML = "辅导员" + title.innerHTML.slice(title.innerHTML.length - 2, title.innerHTML.length)
            title2.innerHTML = "辅导员" + title2.innerHTML.slice(title.innerHTML.length - 2, title.innerHTML.length)
            localStorage.setItem('user', '辅导员')

            loginPath = 'http://127.0.0.1:3007/api/instructorLogin'
            signPath = 'http://127.0.0.1:3007/api/instructorReguser'
            deleteNode()

        })
        // 登录按钮
    $('.form-container').keydown(function(e) {
        if (e.keyCode === 13) {
            console.log(e.keyCode);
            dianji()
        }
    })
    $('.login').click(function() {
        dianji()
    })

    function dianji() { // 点击后获取用户名和密码
        var userdata = $('#username')[0].value;
        var pwddata = $('#password')[0].value;

        login(userdata, pwddata, function(token, message, id) {
            let headers = [{
                id: id,
                Authorization: token,
                message: message,
            }];
            // localStorage.setItem('token', JSON.stringify(headers));

            if (message === '登录成功') {


                // let token = localStorage.getItem("token");
                // token = JSON.parse(token);
                // console.log(token[0].Authorization);
                // 跳转到index页面
                // console.log(headers);
                window.localStorage.setItem('token', JSON.stringify(headers))

                if (window.localStorage.getItem('user') === '学生') {
                    location.replace("http://127.0.0.1:3007/html/学生端")

                } else if (window.localStorage.getItem('user') === '教师') {
                    location.replace("http://127.0.0.1:3007/html/教师端")

                } else if (window.localStorage.getItem('user') === '辅导员') {
                    location.replace("http://127.0.0.1:3007/html/辅导员端")

                }
                // window.sessionStorage.setItem('token', JSON.stringify(headers))
                // console.log(localStorage.getItem('token'));
                // $(location).attr("href","./index.html")
            }
        });
    }
    // 获取token按钮
    // 登录函数
    function login(userdata, pwddata, callback) {
        $.ajax(loginPath, {
            data: {
                username: userdata,
                password: pwddata
            },
            type: 'POST',
            // 响应体结果
            dataType: 'json',
            // 是否异步
            async: true,
            // 成功的回调 此data 是服务器返回的参数
            success: function(data) {
                // console.log(data.message);

                document.getElementById('forgetPwd').innerHTML = data.message;
                callback(data.token, data.message, data.id);
            },
        });
    }

    $('.zhuce').on({
        click: function() {
            var userdata = $('#signUpuser')[0].value;
            var pwddata = $('#signUppwd')[0].value;
            var age = $('.age')[0].value;
            var sex = $('.sex')[0].value;
            var email = $('.email')[0].value;
            var major = $('.major')[0].value;
            var student_id = $('.student_id')[0].value;
            var userclass = $('.userclass')[0].value
            $.ajax(signPath, {
                data: {
                    username: userdata,
                    password: pwddata,
                    age: age,
                    sex: sex,
                    email: email,
                    student_id: student_id,
                    major_name: major,
                    class_name: userclass
                },
                type: 'POST',
                // 响应体结果
                dataType: 'json',
                // 成功的回调 此data 是服务器返回的参数
                success: function(data) {
                    // 创建前去登录的按钮
                    document.querySelector('.beforeLogin').innerHTML = data.message



                },

            })

        }
    })


})